package com.wl.api.controller.business;

import java.util.Objects;
import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.business.CustomerAddrFromDTO;
import com.wl.api.dto.business.CustomerAddrReachDTO;
import com.wl.api.model.business.CustomerAddrFrom;
import com.wl.api.model.business.CustomerAddrReach;
import com.wl.api.service.business.ICustomerAddrReachService;

@RestController
@RequestMapping("/external/customer/addrReach")
public class CustomerAddrReachController extends CommonController {
	 
		@Autowired
		ICustomerAddrReachService addrReachService;
		
		@RequestMapping(value="/manage", method = RequestMethod.POST)
		JsonObject create(@RequestBody CustomerAddrReachDTO param) throws Exception {
			JsonObject json = JsonObject.getInstance();
			String title = "", code =""; 
			boolean pass = true;
			if (validate().or(s -> s.getReachAddrId() != null).test(param)) {
				title = ErrorCodeEnum.param_error.getError_code();
				code = ErrorCodeEnum.param_error.getMessage();
				pass = false;
			}
			if (pass) {
				initData(param);
				CustomerAddrReach entity = BeanUtils.copyProperties(param, CustomerAddrReach.class);
				addrReachService.insert(entity);
				json.setData(entity.getReachAddrId());
			}
			setJsonMessage(json, title, code, pass);
			return json;
		}
		
		@RequestMapping(value="/manage", method= RequestMethod.PUT)
		JsonObject modify(CustomerAddrReachDTO param) throws Exception {
			JsonObject json = JsonObject.getInstance();
			CommonUserDTO user = getCommonUser();
			String title = "", code =""; 
			boolean pass = true;
			if (validate().or(s -> s.getReachAddrId() == null).test(param)) {
				title = ErrorCodeEnum.param_error.getError_code();
				code = ErrorCodeEnum.param_error.getMessage();
				pass = false;
			}
			if (pass) {
				initData(param);
				addrReachService.update(BeanUtils.copyProperties(param, CustomerAddrReach.class), new EntityWrapper<CustomerAddrReach>().where("reach_addr_id={0}", param.getReachAddrId()).and("cust_id={0}", user.getCustId()));
				json.setData(param.getReachAddrId());
			}
			setJsonMessage(json, title, code, pass);
			return json;
		}

		@RequestMapping(value = "/manage/{id}", method = RequestMethod.DELETE)
		JsonObject delete(@PathVariable("id") Integer reachAddrId) throws Exception {
			JsonObject json = JsonObject.getInstance();
			CommonUserDTO user = getCommonUser();
			String title = "", code = "";
			boolean pass = true;
			
			CustomerAddrReach data = addrReachService.selectById(reachAddrId);
			if (Objects.isNull(data) || !Objects.equals(user.getCustId(), data.getCustId())) {
				pass = false;
				title = ErrorCodeEnum.bad_request_messsage;
				code = ErrorCodeEnum.bad_request_code;
			}
			if (pass) {
				json.setData(addrReachService.deleteById(reachAddrId));
			}
			setJsonMessage(json, title, code, pass);
			return json;
		}
		
		@RequestMapping(value = "/manage")
		JsonObject getItemList(CustomerAddrReachDTO param) throws Exception{
			JsonObject json = JsonObject.getInstance();
			CommonUserDTO user = getCommonUser();
			param.setCustId(user.getCustId());
			json.setPage(addrReachService.getAddrReachList(param));
			return json;
		}
		
		@RequestMapping(value="/manage/{id}", method = RequestMethod.GET)
		JsonObject getDetail(@PathVariable("id") Integer reachAddrId) throws Exception {
			JsonObject json = JsonObject.getInstance();
			CommonUserDTO user = getCommonUser();
			CustomerAddrReach data = addrReachService.selectById(reachAddrId);
			if (Objects.equals(user.getCustId(), data.getCustId())) {
				json.setData(data);
			}
			setJsonMessage(json);
			return json;
			
		}
		private void initData(CustomerAddrReachDTO param) throws Exception {
			CommonUserDTO user = getCommonUser();
			param.setCustId(user.getCustId());
		}
		
		private Predicate<CustomerAddrReachDTO> validate() {
		 return s -> StringUtil.isEmpty(s.getEnglishName()) || StringUtil.isEmpty(s.getAddress())
				 || !StringUtil.isEmail(s.getEmail()) || StringUtil.isEmpty(s.getCity()) 
				 || StringUtil.isEmpty(s.getMobile()) || StringUtil.isEmpty(s.getPostcode());
		}
}
